import os
import re
import subprocess
import getpath
import sys
def runCmd(cmd):
    from subprocess import Popen, PIPE, STDOUT
    # cmd="/sqlplus %s/%s@%s:%s/%s" % (db_user,db_password,db_ip,db_port,db_tnsname)
    # cmd = sqlplus_path+cmd
    p = Popen(cmd, stdout=PIPE, stdin=PIPE, stderr=STDOUT)
def mylistdir(p,f):
    a=os.listdir(p)
    fs=myfind(a,f)
    return(fs)
def myfind(l,p):
    lr=[];
    #print p
    p1=p.replace(".",r"\.")
    p2=p1.replace("*",".*")
    p2=p2+"$"
    for a in l:
        #print a
        if  re.search(p2,a,re.IGNORECASE)==None :
           pass
           #print "pass"
        else:
           lr.append(a)
       #print "append"
    return lr    
def  getfiles():
    return mylistdir(".","*.ui")
def uiCone(f):    
    f=os.path.splitext(f)
    if sys.platform == "linux":
        cmd='pyuic5  %s.ui >ui_%s.py' %(f[0],f[0])
    else:    
        # cmd=r'%s\Python34\Lib\site-packages\PyQt5\uic.exe -g python  %s.ui >ui_%s.py' %(getpath.getdrive(),f[0],f[0])
        # cmd=r'%s\Python311\Lib\site-packages\PyQt5\pyuic5  %s.ui >ui_%s.py' %(getpath.getdrive(),f[0],f[0])
        exe=r'%s\Python311\scripts\pyuic5.exe' % getpath.getdrive()
        if os.path.exists(exe):
            cmd=r"""%s %s.ui>ui_%s.py""" % (exe,f[0],f[0])
        else:
            exe=r'%s\Python38\scripts\pyuic5.exe' % getpath.getdrive()
            cmd=r"""%s %s.ui>ui_%s.py""" % (exe,f[0],f[0])
    print(cmd)
    os.system(cmd)
    # runCmd(cmd)
    return cmd
def uic():
    fs=getfiles()
    # out=open("uic.sh","w")
    for f in fs:
        uiCone(f)
    #     out.write(uiCone(f)+"\n")
    # out.close()
def  getfiles_rc():
    return mylistdir(".","*.qrc")
def rCone(f):        
    f=os.path.splitext(f)
    if sys.platform == "linux":
        cmd=r'pyrcc5  %s.qrc >%s_rc.py' %(f[0],f[0])
    else:
        # cmd=r'%s\Python34\Lib\site-packages\PyQt5\rcc.exe -g python  %s.qrc >%s_rc.py' %(getpath.getdrive(),f[0],f[0])
        # cmd=r'%s\Python311\Lib\site-packages\PyQt5\pyrcc5  %s.qrc >%s_rc.py' %(getpath.getdrive(),f[0],f[0])
        # cmd=r'%s\Python311\scripts\pyrcc5  %s.qrc >%s_rc.py' %(getpath.getdrive(),f[0],f[0])
        # cmd=r'%s\Python38\scripts\pyuic5.exe  %s.ui >ui_%s.py' %(getpath.getdrive(),f[0],f[0])
        # cmd=r'/usr/bin/pyrcc5  %s.qrc >%s_rc.py' %(f[0],f[0])
        exe=r'%s\Python311\scripts\pyrcc5.exe' % getpath.getdrive()
        if os.path.exists(exe):
            cmd=r"""%s %s.qrc >%s_rc.py""" % (exe,f[0],f[0])
        else:
            exe=r'%s\Python38\scripts\pyrcc5.exe' % getpath.getdrive()
            cmd=r"""%s %s.qrc >%s_rc.py""" % (exe,f[0],f[0])

    print(cmd)
    os.system(cmd)
    # runCmd(cmd)
    return cmd
def rc():
    fs=getfiles_rc()
    # out=open("uic.sh","w")
    for f in fs:
        rCone(f)
uic()
rc()
